package com.edeweiss.community.servlet;

import com.edeweiss.community.domain.*;
import com.edeweiss.community.services.CommunityService;
import com.edeweiss.community.services.servicesImp.CommunityServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/CommunityServlet")
public class CommunityServlet extends BaseServlet{
    private CommunityService service = new CommunityServiceImpl();

    //添加社团
    public String addCommunity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入数据的社团名称
        String cname = request.getParameter("cname");
        String responsibility = request.getParameter("responsibility");
        String ccategory = request.getParameter("ccategory");
        String ctime = request.getParameter("ctime");
        String cteacher = request.getParameter("cteacher");
        String cintroduce = request.getParameter("cintroduce");

        Community community = new Community();
        community.setCname(cname);
        community.setCcategory(Integer.parseInt(ccategory));
        community.setCintroduce(cintroduce);
        community.setCteacher(cteacher);
        community.setResponsibility(responsibility);
        //将字符串转换成日期格式
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = df.parse(ctime);
            community.setCtime(date);
        }catch (Exception e){
            throw new RuntimeException("日期格式有误");
        }
        //调用service方法进行添加社团
        int flag = service.addCommunity(community);
        if(flag == 1){
            request.getSession().setAttribute("msg","添加社团成功");
        }else {
            request.getSession().setAttribute("msg","添加失败，请重试");
        }
        //返回结果
        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //添加社团的分类
    public String addCommunityCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String cgid = request.getParameter("cgid");
        String cgname = request.getParameter("cgname");

        Category cg = new Category();
        cg.setCgid(Integer.parseInt(cgid));
        cg.setCgname(cgname);
        //调用service层进行添加分类
        int flag = service.addCommunityCategory(cg);
        if(flag == 1){
            request.getSession().setAttribute("msg","添加成功");
        }else {
            request.getSession().setAttribute("msg","添加失败，请重试");
        }

        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //前台查询社团分类
    public String findCommunityCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categories = service.findCommunityCategory();
        request.getSession().setAttribute("categories",categories);
        response.sendRedirect("/association.jsp");
        //返回到展示数据的页面
        return "";
    }

    //后台查询社团分类
    public String findCommunityCategoryAfter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categories = service.findCommunityCategory();
        if(categories != null && categories.size() >0){
            request.getSession().setAttribute("categories",categories);
            //返回到展示数据的页面
            response.sendRedirect("pages/addCommunity.jsp");
        }else {
            request.getSession().setAttribute("msg","查询失败");
            response.sendRedirect("pages/index.jsp");
        }
        return "";
    }
    //查询所有社团
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service查询
        List<Community> all = service.findAll();
        if(all != null && all.size() >0){
            request.getSession().setAttribute("all",all);
            //返回到展示数据的页面
            response.sendRedirect("pages/findAllCommunity.jsp");
        }else {
            request.getSession().setAttribute("msg","查询失败");
            response.sendRedirect("pages/index.jsp");
        }
        return "";
    }
    //根据分类id查询社团
    public String findCommunityByCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取分类id
        String cgid = request.getParameter("cgid");
        List<Community> communities = service.findCommunityByCategory(Integer.parseInt(cgid));
        if(communities != null && communities.size() >0){
            request.getSession().setAttribute("communities",communities);
            //返回到展示数据的页面
        }else {
            request.getSession().setAttribute("msg","查询失败");
        }
        response.sendRedirect("/association.jsp");
        return "";
    }
    //根据名称查询社团
    public String findCommunityByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("cname");
        //查询社团
        Community community = service.findCommunityByName(name);
        if(community != null){
            //根据社团查询社团举办过的活动
            List<Activity> as = service.findActivity(community.getCid());
            HttpSession session = request.getSession();
            session.setAttribute("community",community);
            session.setAttribute("as",as);
            response.sendRedirect("communityIndex.jsp");
        }else {
            request.setAttribute("msg","你所查询的社团不存在！");
        }
        //返回到展示数据的页面
        return "association.jsp";
    }
    //管理员同意解散社团
    public String deleteCommunity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        int flag = service.deleteCommunity(Integer.parseInt(cid));
        if(flag == 1){
            request.getSession().setAttribute("msg","解散社团成功");
        }else {
            request.getSession().setAttribute("msg","解散失败，请重试");
        }
        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //社团社长申请加入荣誉墙
    public String honorWallApprove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("community_id");
        String material = request.getParameter("material");

        HonorWall honorWall = new HonorWall();
        honorWall.setCommunity_id(Integer.parseInt(cid));
        honorWall.setMaterial(material);
        honorWall.setDate(new Date());
        //设置为未处理
        honorWall.setStatus(0);

        //调用service层进行提交
        int flag = service.honorWallApprove(honorWall);
        if(flag == 1){
            request.getSession().setAttribute("msg","申请成功，等待管理员审核！");
        }else{
            request.getSession().setAttribute("msg","申请失败，请再次尝试！");
        }
        //请求重定向
        response.sendRedirect("community/index.jsp");
        return "";
    }

    //社长申请解散社团
    public String dissolveCommunity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("community_id");
        String material = request.getParameter("material");

        //申请加入荣誉墙和解散社团属于一样，在此借用申请加入荣誉墙的实体类
        HonorWall honorWall = new HonorWall();
        honorWall.setCommunity_id(Integer.parseInt(cid));
        honorWall.setMaterial(material);
        honorWall.setDate(new Date());
        //设置为未处理
        honorWall.setStatus(0);

        //调用service层进行提交
        int flag = service.dissolveCommunity(honorWall);
        if(flag == 1){
            request.getSession().setAttribute("msg","申请成功，等待管理员审核！");
        }else{
            request.getSession().setAttribute("msg","申请失败，请再次尝试！");
        }
        //请求重定向
        response.sendRedirect("community/index.jsp");
        return "";
    }

    //对入社人员进行查询
    public String findApprovePerson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取社团的id，查询自己社团的申请人
        String cid = request.getParameter("cid");
        List<CommunityApply> persons = service.findApprovePerson(Integer.parseInt(cid));
        if(persons != null && persons.size() > 0) {
            request.getSession().setAttribute("persons",persons);
            response.sendRedirect("community/joinCommunityApprove.jsp");
        }else{
            request.getSession().setAttribute("msg","没有申请的学生！");
            //请求重定向
            response.sendRedirect("community/index.jsp");
        }
        return "";
    }

    //对入社人员进行审核
    public String approvePerson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理的那条信息
        String cid = request.getParameter("cid");
        //处理结果
        String result = request.getParameter("result");
        //获取学生的id
        String sid = request.getParameter("sid");
        //获取社团id
        String cname = request.getParameter("cname");

        int flag = service.approvePerson(Integer.parseInt(cid),Integer.parseInt(result),sid,Integer.parseInt(cname));

        if(flag == 1) {
            request.getSession().setAttribute("msg","处理成功，请接着处理下一条请求");
        }else{
            request.getSession().setAttribute("msg","没有申请的学生！");
            //请求重定向
        }
        response.sendRedirect("community/index.jsp");
        return "";
    }

    //查询要申请参加活动的学生
    public String findJoinActivity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取查询社团的id
        String cid = request.getParameter("cid");
        List<FindActivity> joinActivitys = service.findJoinActivity(Integer.parseInt(cid));
        if(joinActivitys != null && joinActivitys.size() >0){
            request.getSession().setAttribute("joinActivitys",joinActivitys);
            response.sendRedirect("community/joinActivityApprove.jsp");
        }else {
            request.getSession().setAttribute("msg","没有申请活动的学生！");
            response.sendRedirect("community/index.jsp");
        }
        return "";
    }

    //处理要参加活动的学生
    public String joinActivityApprove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获或参加活动和学生的id
        String sid = request.getParameter("sid");
        String aid = request.getParameter("aid");
        String result = request.getParameter("result");

        int flag = service.joinActivityApprove(Integer.parseInt(aid), sid, Integer.parseInt(result));
        if(flag==1){
            request.getSession().setAttribute("msg","处理成功，请接着处理下一条请求！");
        }else {
            request.getSession().setAttribute("msg","处理失败，请重新处理！");
        }
        response.sendRedirect("community/index.jsp");
        return "";
    }

    //查询所有社员
    public String findMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取社团id
        String cid = request.getParameter("cid");
        List<Student> members = service.findMember(Integer.parseInt(cid));
        if(members != null && members.size()>0){
            request.getSession().setAttribute("members",members);
            response.sendRedirect("community/findAllMember.jsp");
        }else {
            request.getSession().setAttribute("msg","你的社团还没有任何成员");
            response.sendRedirect("community/index.jsp");
        }
        return "";
    }
    //删除社员
    public String delMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取社员id和社团id
        String sid = request.getParameter("sid");
        String cid = request.getParameter("cid");
        int flag = service.delMember(sid,Integer.parseInt(cid));
        if(flag == 1){
            request.getSession().setAttribute("msg","删除成功");
        }else {
            request.getSession().setAttribute("msg","删除失败，请重新尝试！");
        }
        response.sendRedirect("community/index.jsp");
        return "";
    }

    //查询社团消息
    public String findMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取社员id和社团id
        String cid = request.getParameter("cid");
        List<MessageCommunity> mcs = service.findMessage(Integer.parseInt(cid));
        if(mcs != null && mcs.size()>0){
            request.getSession().setAttribute("mcs",mcs);
            response.sendRedirect("community/letter.jsp");
        }else {
            request.getSession().setAttribute("msg","没有消息！");
            response.sendRedirect("community/index.jsp");
        }
        return "";
    }
}
